﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class CsMaintanceProjectBll
    {
        private static object o = new object();

        public DataTable GetMaintanceProjectSync(String OrganIdentifier)
        {
            Sync(OrganIdentifier);
            CsMaintanceProject Project = new CsMaintanceProject();
            Project.ProOrganIdentifier = OrganIdentifier;
            SqlWhere where = new SqlWhere();
            where.Info = Project;
            return CsMaintanceProject.Select(where, GlobalConfig.ConnectionString);
        }

        private void Sync(String OrganIdentifier)
        {
            UpdateSync(OrganIdentifier);
            InsertSync(OrganIdentifier);
        }
        private void UpdateSync(String OrganIdentifier)
        {
            //同步已经存在的维修项目信息以及维修项目类别信息
            DataTable dt_Sync = GetProjectData(OrganIdentifier);

            #region 同步维修项目类别
            DataTable dt = FactoryClass.CreateCsSort().GetSortIdentifierNotNull(@"Maintenance");
            String Rtidentity = @"App_Identifier";
            String idtntity = @"SortIdentitfier";
            String EnmunType = @"CsSortColumnDictionary";
            RoadTeam.Pub.SyncTable.SyncDataTable(dt, dt_Sync, Rtidentity, idtntity, EnmunType);
            #endregion

            #region 同步维修项目
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            dt = FactoryClass.CreateCsProject().GetProjectIdentifierNotNull(@"CsMaintenance", OrganID);
            Rtidentity = @"App_Identifier";
            idtntity = @"ProjectIdentitfier";
            EnmunType = @"CsProjectColumnDictionary";
            RoadTeam.Pub.SyncTable.SyncDataTable(dt, dt_Sync, Rtidentity, idtntity, EnmunType);
            #endregion
            CsMaintanceProject.Update(dt_Sync, GlobalConfig.ConnectionString);
        }
        private void InsertSync(String OrganIdentifier)
        {
            //同步未存在的维修项目信息以及维修项目类别信息
            DataTable dt = GetProjectData("-1");
            DataRow dr;
            lock (o)
            {
                String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
                DataTable dt_Project = FactoryClass.CreateCsProject().GetProjectIdentifierNull("CsMaintenance", OrganID);
                String StrWhere = String.Empty;
                List<string> li = new List<string>();
                foreach (DataRow Temp in dt_Project.Rows)
                {
                    if (li.Contains(Temp[3].ToString())) continue;
                    li.Add(Temp[3].ToString());
                    StrWhere += Temp[3].ToString() + ",";
                }
                DataTable dt_Sort = FactoryClass.CreateCsSort().GetSortBySortIDs("Maintenance", StrWhere.TrimEnd(','));
                foreach (DataRow dr_Project in dt_Project.Rows)
                {
                    dr = dt.NewRow();
                    dr["MainProIdentifier"] = CreateIdentifier.GetIdentifier();
                    DataRow[] dr_Sorts = dt_Sort.Select(@"ID=" + dr_Project["SortID"].ToString());
                    if (dr_Sorts.Count() > 0)
                    {
                        if (dr_Sorts[0][3] != null && !String.IsNullOrEmpty(dr_Sorts[0][3].ToString()))
                            dr["SortIdentitfier"] = dr_Sorts[0][3].ToString();
                        else
                        {
                            String SortIdentitfier = CreateIdentifier.GetIdentifier();
                            dr["SortIdentitfier"] = SortIdentitfier;
                            dr_Sorts[0][3] = SortIdentitfier;
                        }
                        dr["SortName"] = dr_Sorts[0][2];
                        dr["SortCode"] = dr_Sorts[0][1];
                    }
                    //ProjectID,ProjectCode,ProjectName,SortID,OrganID,App_Identifier
                    String ProjectIdentitfier = CreateIdentifier.GetIdentifier();
                    dr["ProjectIdentitfier"] = ProjectIdentitfier;
                    dr_Project[5] = ProjectIdentitfier;

                    dr["ProjectIdentitfier"] = ProjectIdentitfier;
                    dr["ProjectName"] = dr_Project[2];
                    dr["ProjectCode"] = dr_Project[1];
                    dr["ProOrganIdentifier"] = OrganIdentifier;
                    dt.Rows.Add(dr);
                }
                FactoryClass.CreateCsProject().UpdateProject(dt_Project);
                FactoryClass.CreateCsSort().UpdateSort(dt_Sort);
                CsMaintanceProject.InsertDatatable(dt, GlobalConfig.ConnectionString);
            }
        }
        public DataTable GetProjectData(String OrganIdentifier)
        {
            SqlWhere where = new SqlWhere();
            where.Info = new CsMaintanceProject() { ProOrganIdentifier = OrganIdentifier };
            where.SelectColumn = @"MainProIdentifier,SortIdentitfier,SortName,SortCode,ProjectIdentitfier,
ProjectName,ProjectCode,ProOrganIdentifier";
            return CsMaintanceProject.Select(where, GlobalConfig.ConnectionString);
        }
    }
}
